package pres.wchen.bussiness.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import pres.wchen.bussiness.cor.model.BaseModel;
import pres.wchen.bussiness.cor.util.DateUtils;
import pres.wchen.bussiness.service.BusConfigService;
import pres.wchen.bussiness.service.BusProjectVersionService;
import pres.wchen.bussiness.vo.BusConfigVO;
import pres.wchen.bussiness.vo.BusInfoVO;
import pres.wchen.bussiness.service.BusInfoService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import pres.wchen.bussiness.vo.BusProjectVersionVO;
import pres.wchen.bussiness.web.BaseController;

/**
 * @作者： wchen
 * @描述： 商户信息表管理
 * @创建时间： 2018/02/18 00:26:42
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */
@RestController
@RequestMapping("/bus/business")
public class BusInfoController extends BaseController {
	@Autowired
	private BusInfoService busInfoService;

	@Autowired
	private BusProjectVersionService busProjectVersionService;

	@Autowired
	private BusConfigService busConfigService;

	/**
     *  @描述: 商户信息表新增
     *  @参数： [busInfoVO]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/18
     *  @修改人和其它信息:
     */
	@PostMapping(value = "/add")
	public Map insert(BusInfoVO busInfoVO) {
		Long id = busInfoService.insert(busInfoVO);
		//版本记录添加
		String [] types = {"admin","client","sys","mall","website","time"};
		for(String type : types){
			BusProjectVersionVO versionVO = new BusProjectVersionVO();
			versionVO.setBusId(busInfoVO.getId());
			versionVO.setCreateTime(DateUtils.getStrCurrtTime());
			versionVO.setProjectType(type);
			busProjectVersionService.insert(versionVO);
		}
		//服务器配置 	BUS_CONFIG_TENCENT
//		if(busInfoVO.getServerIp() != null) {
//			BusConfigVO busConfigVO = new BusConfigVO();
//			busConfigVO.setCreateTime(DateUtils.getStrCurrtTime());
//			busConfigVO.setConsName("BUS_CONFIG_TENCENT");
//			busConfigVO.setConfigName("IP地址");
//			busConfigVO.setConfigValue(busInfoVO.getServerIp());
//			busConfigService.insert(busConfigVO);
//		}
		return super.dealInsertResult(id);
	}


	/**
     *  @描述: 商户信息表删除
     *  @参数： [id]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/18
     *  @修改人和其它信息:
     */
	@DeleteMapping(value = "/delete/{id}")
	public Map delete(@PathVariable("id") Long id) {
		Integer result = busInfoService.delete(id);
		//版本记录删除
		if(result != 0) {
			busProjectVersionService.delete(id);
		}
		return super.dealDeleteResult(result);
	}


	/**
     *  @描述: 商户信息表修改
     *  @参数： [busInfoVO]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/18
     *  @修改人和其它信息:
     */
	@PutMapping(value = "/modify/")
	public Map modify(BusInfoVO busInfoVO) {
		String status = busInfoVO.getStatus();
		if(status != null && status.equals("1")){
			busInfoVO.setFinishTime(DateUtils.getStrCurrtTime());
		}else if(status != null && status.equals("0")){
			busInfoVO.setFinishTime("");
		}

		Integer result = busInfoService.update(busInfoVO);

		return super.dealUpdateResult(result);
	}


	/**
     *  @描述: 商户信息表详情
     *  @参数： [id]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/18
     *  @修改人和其它信息:
     */
	@GetMapping(value = "/info/{id}")
	public ModelAndView info(@PathVariable("id") Long id) {
	    Map conditionMap = new HashMap();
        conditionMap.put("id", id);
		BusInfoVO busInfoVO = busInfoService.info(conditionMap);
		return super.dealInfoResult("/bus/business/maintain_modify", busInfoVO);
	}


	/**
     *  @描述: 商户信息表分页列表
     *  @参数： [request]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/2/18
     *  @修改人和其它信息:
     */
	@GetMapping(value = "/pageList")
	public ModelAndView pageList(HttpServletRequest request) {
		Object pageSize = request.getParameter("pageSize");
		Object pageNo = request.getParameter("pageNo");
		Object busName = request.getParameter("busName");
		Object pageFlag = request.getParameter("pageFlag");
		Map conditionMap = new HashMap();
		conditionMap.put("pageSize", pageSize);
		conditionMap.put("pageNo", pageNo);
		conditionMap.put("busName", busName);
		conditionMap.put("pageFlag", pageFlag);
		BaseModel baseModel = busInfoService.list(conditionMap);
		return super.dealListResult("/bus/business/maintain", baseModel);
	}

}
